package com.ctl.szmd.szmd.feature_wm.ui.fragment.pager1

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.paging.LoadState
import com.ctl.lib_common.extensions.singleClick
import com.ctl.lib_common.widget.recyclerview.MyDecoration
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.bean.FulianSubOrgInfoForListForApiList
import com.ctl.szmd.szmd.feature_wm.databinding.FragmentSubOrgBinding
import com.ctl.szmd.szmd.feature_wm.ui.adapter.footer.FooterAdapter
import com.ctl.szmd.szmd.feature_wm.ui.adapter.p1.SubOrgInfoAdapter
import com.ctl.szmd.szmd.feature_wm.ui.base.BaseWmBindingFragment
import com.ctl.szmd.szmd.feature_wm.ui.viewmodel.pager1.SubOrgViewModel
import kotlinx.android.synthetic.main.fragment_sub_org.*
import kotlinx.android.synthetic.main.view_wm_tool_bar.*
import kotlinx.coroutines.flow.collectLatest
import org.koin.android.viewmodel.ext.android.viewModel


class SubOrgFragment : BaseWmBindingFragment<FragmentSubOrgBinding>() {

    companion object {
        const val DEP_ID = "dep_id"
    }

    private val viewModel: SubOrgViewModel by viewModel()
    private val mAdapter by lazy { SubOrgInfoAdapter() }
    private var dep_id: String? = null

    override fun getLayoutId(): Int {
        return R.layout.fragment_sub_org
    }

    override fun initView() {
        toolbar_title.text = "下属组织"
        toolbar_left.visibility = View.VISIBLE
        toolbar_left.singleClick {
            findNavController().navigateUp()
        }
        if (arguments?.getString(DEP_ID) != null) {
            dep_id = arguments?.getString(DEP_ID)
        }
        if (dep_id == null) {
            toolbar.visibility = View.GONE
        }

        binding.apply {
            vm = viewModel
            lifecycle.addObserver(viewModel)

            recycler_view.addItemDecoration(MyDecoration())
            recycler_view.adapter = mAdapter.withLoadStateFooter(FooterAdapter { mAdapter.retry() })

            refreshLayout.setOnRefreshListener {
                mAdapter.refresh()
                refreshLayout.isRefreshing = false
            }
            lifecycleScope.launchWhenCreated {
                mAdapter.loadStateFlow
                    .collectLatest { state ->
                        refresh_layout.isRefreshing = state.refresh is LoadState.Loading
                    }
            }
            mAdapter.listener = View.OnClickListener {
                if (it.tag is FulianSubOrgInfoForListForApiList) {
                    val data = it.tag as FulianSubOrgInfoForListForApiList
                    if (data.IsLeaf != null) {
                        if (data.IsLeaf == 1) {
                            val bundle = Bundle()
                            bundle.putString(SubOrgDetailFragment.TITLE, data.DepName)
                            bundle.putString(SubOrgDetailFragment.BRIEF_INTRO, data.DepIntro)
                            bundle.putString(SubOrgDetailFragment.IMAGE_NET_PATH, data.ImgNetPath)
                            parentFragment?.findNavController()
                                ?.navigate(R.id.subOrgDetailFragment, bundle)
                        } else {
                            val bundle = Bundle()
                            bundle.putString(DEP_ID, data.DepId)
                            parentFragment?.findNavController()
                                ?.navigate(R.id.subOrgFragment, bundle)
                        }
                    }
                }
            }

            viewModel.apply {
                _emptyPageEvent.observe(viewLifecycleOwner, Observer {
                    if (it) {
                        baseViewEmpty.visibility = View.VISIBLE
                    } else {
                        baseViewEmpty.visibility = View.GONE
                    }
                })
                getListData(dep_id).observe(viewLifecycleOwner, Observer {
                    mAdapter.submitData(lifecycle, it)
                })
            }
        }
    }
}